<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/overloads.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../function_invocation_and_creation.html" title="Chapter 4. Function Invocation and Creation">
<link rel="prev" href="boost_python_make_function_hpp.html" title="boost/python/make_function.hpp">
<link rel="next" href="boost_python_ptr_hpp.html" title="boost/python/ptr.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_make_function_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_ptr_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="function_invocation_and_creation.boost_python_overloads_hpp"></a><a class="link" href="boost_python_overloads_hpp.html" title="boost/python/overloads.hpp">boost/python/overloads.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.overloaddispatcher_concept">OverloadDispatcher
        Concept</a></span></dt>
<dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.macros">Macros</a></span></dt>
<dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_overloads_hpp.introduction"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<div class="toc"><dl class="toc"><dt><span class="section"><a href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions">overload-dispatch-expressions</a></span></dt></dl></div>
<p>
          Defines facilities for generating families of overloaded Python functions
          and extension class methods from C++ functions and member functions with
          default arguments, or from similar families of C++ overloads
        </p>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.introduction.overload_dispatch_expressions" title="overload-dispatch-expressions">overload-dispatch-expressions</a>
</h4></div></div></div>
<p>
            An overload-dispatch-expression is used to describe a family of overloaded
            methods to be generated for an extension class. It has the following
            properties:
          </p>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">docstring</span></dt>
<dd><p>
                  An <a class="link" href="../glossary.html#ntbs">ntbs</a> whose value will bound to the
                  methods' <code class="computeroutput"><span class="identifier">__doc__</span></code>
                  attribute
                </p></dd>
<dt><span class="term">keywords</span></dt>
<dd><p>
                  A <a class="link" href="../function_invocation_and_creation.html#function_invocation_and_creation.boost_python_args_hpp.introduction.keyword_expressions" title="keyword-expressions">keyword-expression</a>
                  which will be used to name (a trailing subsequence of) the arguments
                  to the generated methods.
                </p></dd>
<dt><span class="term">call policies</span></dt>
<dd><p>
                  An instance of some type which models CallPolicies.
                </p></dd>
<dt><span class="term">minimum arity</span></dt>
<dd><p>
                  The minimum number of arguments to be accepted by a generated method
                  overload.
                </p></dd>
<dt><span class="term">maximum arity</span></dt>
<dd><p>
                  The maximum number of arguments to be accepted by a generated method
                  overload.
                </p></dd>
</dl>
</div>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_overloads_hpp.overloaddispatcher_concept"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.overloaddispatcher_concept" title="OverloadDispatcher Concept">OverloadDispatcher
        Concept</a>
</h3></div></div></div>
<p>
          An OverloadDispatcher X is a class which has a minimum arity and a maximum
          arity, and for which the following following are valid overload-dispatch-expressions,
          with the same minimum and maximum arity as the OverloadDispatcher.
        </p>
<pre class="programlisting"><span class="identifier">X</span><span class="special">()</span>
<span class="identifier">X</span><span class="special">(</span><span class="identifier">docstring</span><span class="special">)</span>
<span class="identifier">X</span><span class="special">(</span><span class="identifier">docstring</span><span class="special">,</span> <span class="identifier">keywords</span><span class="special">)</span>
<span class="identifier">X</span><span class="special">(</span><span class="identifier">keywords</span><span class="special">,</span> <span class="identifier">docstring</span><span class="special">)</span>
<span class="identifier">X</span><span class="special">()[</span><span class="identifier">policies</span><span class="special">]</span>
<span class="identifier">X</span><span class="special">(</span><span class="identifier">docstring</span><span class="special">)[</span><span class="identifier">policies</span><span class="special">]</span>
<span class="identifier">X</span><span class="special">(</span><span class="identifier">docstring</span><span class="special">,</span> <span class="identifier">keywords</span><span class="special">)[</span><span class="identifier">policies</span><span class="special">]</span>
<span class="identifier">X</span><span class="special">(</span><span class="identifier">keywords</span><span class="special">,</span> <span class="identifier">docstring</span><span class="special">)[</span><span class="identifier">policies</span><span class="special">]</span>
</pre>
<p>
          * If policies are supplied, it must be an instance of a type which models
          <a class="link" href="../concepts.html#concepts.callpolicies" title="CallPolicies">CallPolicies</a>, and will be
          used as the result's call policies. Otherwise the result's call policies
          will be an instance of <a class="link" href="models_of_callpolicies.html#function_invocation_and_creation.models_of_callpolicies.boost_python_default_call_polici" title="boost/python/default_call_policies.hpp"><code class="computeroutput"><span class="identifier">default_call_policies</span></code></a>. * If docstring
          is supplied it must be an <a class="link" href="../glossary.html#ntbs">ntbs</a>, and will be
          used as the result's docstring. Otherwise the result has an empty docstring.
          * If keywords is supplied it must be the result of a <a class="link" href="../function_invocation_and_creation.html#function_invocation_and_creation.boost_python_args_hpp.introduction.keyword_expressions" title="keyword-expressions">keyword-expression</a>
          whose length is no greater than X's maximum arity, and will be used as
          the result's keywords. Otherwise the result's keywords will be empty.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_overloads_hpp.macros"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.macros" title="Macros">Macros</a>
</h3></div></div></div>
<pre class="programlisting"><span class="identifier">BOOST_PYTHON_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">name</span><span class="special">,</span> <span class="identifier">func_id</span><span class="special">,</span> <span class="identifier">min_args</span><span class="special">,</span> <span class="identifier">max_args</span><span class="special">)</span>
</pre>
<p>
          Expands to the definition of an OverloadDispatcher called name in the current
          scope which can be used to generate the following function invocation:
        </p>
<pre class="programlisting"><span class="identifier">func_id</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">,...</span><span class="identifier">ai</span><span class="special">);</span></pre>
<p>
          for all <code class="computeroutput"><span class="identifier">min_args</span> <span class="special">&lt;=</span>
          <span class="identifier">i</span> <span class="special">&lt;=</span>
          <span class="identifier">max_args</span></code>.
        </p>
<pre class="programlisting"><span class="identifier">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">name</span><span class="special">,</span> <span class="identifier">member_name</span><span class="special">,</span> <span class="identifier">min_args</span><span class="special">,</span> <span class="identifier">max_args</span><span class="special">)</span>
</pre>
<p>
          Expands to the definition of an OverloadDispatcher called name in the current
          scope which can be used to generate the following function invocation:
        </p>
<pre class="programlisting"><span class="identifier">x</span><span class="special">.</span><span class="identifier">member_name</span><span class="special">(</span><span class="identifier">a1</span><span class="special">,</span> <span class="identifier">a2</span><span class="special">,...</span><span class="identifier">ai</span><span class="special">);</span></pre>
<p>
          for all min_args &lt;= i &lt;= max_args, where x is a reference to an object
          of class type.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="function_invocation_and_creation.boost_python_overloads_hpp.example"></a><a class="link" href="boost_python_overloads_hpp.html#function_invocation_and_creation.boost_python_overloads_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">def</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">args</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">tuple</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">overloads</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">return_internal_reference</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>

<span class="identifier">tuple</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span> <span class="special">=</span> <span class="number">1</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">4.25</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">z</span> <span class="special">=</span> <span class="string">"wow"</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">return</span> <span class="identifier">make_tuple</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">y</span><span class="special">,</span> <span class="identifier">z</span><span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_PYTHON_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">f_overloads</span><span class="special">,</span> <span class="identifier">f</span><span class="special">,</span> <span class="number">0</span><span class="special">,</span> <span class="number">3</span><span class="special">)</span>

<span class="keyword">struct</span> <span class="identifier">Y</span> <span class="special">{};</span>
<span class="keyword">struct</span> <span class="identifier">X</span>
<span class="special">{</span>
    <span class="identifier">Y</span><span class="special">&amp;</span> <span class="identifier">f</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">y</span> <span class="special">=</span> <span class="number">4.25</span><span class="special">,</span> <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">z</span> <span class="special">=</span> <span class="string">"wow"</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">inner</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="identifier">Y</span> <span class="identifier">inner</span><span class="special">;</span>
<span class="special">};</span>

<span class="identifier">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class="special">(</span><span class="identifier">f_member_overloads</span><span class="special">,</span> <span class="identifier">f</span><span class="special">,</span> <span class="number">1</span><span class="special">,</span> <span class="number">3</span><span class="special">)</span>

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">args_ext</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">def</span><span class="special">(</span><span class="string">"f"</span><span class="special">,</span> <span class="identifier">f</span><span class="special">,</span>
        <span class="identifier">f_overloads</span><span class="special">(</span>
            <span class="identifier">args</span><span class="special">(</span><span class="string">"x"</span><span class="special">,</span> <span class="string">"y"</span><span class="special">,</span> <span class="string">"z"</span><span class="special">),</span> <span class="string">"This is f's docstring"</span>
        <span class="special">));</span>


    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">Y</span><span class="special">&gt;(</span><span class="string">"Y"</span><span class="special">)</span>
        <span class="special">;</span>

    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">&gt;(</span><span class="string">"X"</span><span class="special">,</span> <span class="string">"This is X's docstring"</span><span class="special">)</span>
        <span class="special">.</span><span class="identifier">def</span><span class="special">(</span><span class="string">"f1"</span><span class="special">,</span> <span class="special">&amp;</span><span class="identifier">X</span><span class="special">::</span><span class="identifier">f</span><span class="special">,</span>
                <span class="identifier">f_member_overloads</span><span class="special">(</span>
                    <span class="identifier">args</span><span class="special">(</span><span class="string">"x"</span><span class="special">,</span> <span class="string">"y"</span><span class="special">,</span> <span class="string">"z"</span><span class="special">),</span> <span class="string">"f's docstring"</span>
                <span class="special">)[</span><span class="identifier">return_internal_reference</span><span class="special">&lt;&gt;()]</span>
        <span class="special">)</span>
        <span class="special">;</span>
<span class="special">}</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_python_make_function_hpp.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../function_invocation_and_creation.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_ptr_hpp.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
